const sdk = require('weapps-plugin-citycode')

export default {
  onPageLoad(query) {
    //console.log('---------> LifeCycle onPageLoad', query)
  },
  onPageShow() {
    //console.log('---------> LifeCycle onPageShow')
    $page.handler.getWeather();

    let token = app.getStorageSync('token');
    
    if (token) {
        $page.state.token = token;
    } else {
        const uid = app.getStorageSync('tif.uid')
        //后端临时url
        const url = `${app.state.tempRequestUrl}wx/auth/login_by_public_side?uid=${uid}`

        if (uid) {
            sdk.request({
                url,
                method: 'POST',
                success: resp => {
                    if (resp.statusCode == 200) {
                        if (resp.data.code == 1) {
                            $page.state.token = resp.data.data.token
                            app.setStorageSync('token', resp.data.data.token)
                        }
                    }
                },
                fail: err => {
                    console.log('err', err)
                }
            })
        }
    }
  },
  onPageReady() {
    //console.log('---------> LifeCycle onPageReady')
  },
  onPageHide() {
    //console.log('---------> LifeCycle onPageHide')
  },
  onPageUnload() {
    //console.log('---------> LifeCycle onPageUnload')
  },
};